package cn.exrick.controller;

import cn.exrick.common.exception.NeedLoginException;
import cn.exrick.common.exception.XmallException;
import cn.exrick.pojo.TbFeedback;
import cn.exrick.pojo.TbFeedbackCat;
import cn.exrick.pojo.common.DataTablesResult;
import cn.exrick.pojo.common.Result;
import cn.exrick.pojo.front.Member;
import cn.exrick.service.FeedbackCatService;
import cn.exrick.service.FeedbackService;
import cn.exrick.service.LoginService;
import cn.exrick.utils.ResultUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 用户反馈
 */
@RestController
@Api(description= "用户反馈")
public class FeedbackFrontController {

    @Autowired
    private FeedbackService feedbackService;
    @Autowired
    private FeedbackCatService feedbackCatService;
    @Autowired
    private LoginService loginService;

    @RequestMapping(value = "/feedback/submit", method = RequestMethod.POST)
    @ApiOperation(value = "提交反馈")
    public Result<Object> getMemberList(String token, String content, Integer category, String image){
        Member member = loginService.getUserByToken(token);
        if (member == null) {
            throw new NeedLoginException();
        }
        if(StrUtil.isBlank(content)){
            throw new XmallException("反馈内容不能为空");
        }
        if(null == category){
            throw new XmallException("反馈类型不能为空");
        }

        TbFeedback tbFeedback = new TbFeedback();
        tbFeedback.setContent(content);
        tbFeedback.setFeedbackCatId(category);
        tbFeedback.setImage(image);
        tbFeedback.setMemberId(Integer.parseInt(String.valueOf(member.getId())));
        tbFeedback.setCreated(new Date());

        feedbackService.add(tbFeedback);

        return new ResultUtil<Object>().setData(true);
    }

    @RequestMapping(value = "/feedback/getCategories",method = RequestMethod.GET)
    @ApiOperation(value = "获取所有反馈分类")
    public Result<List<TbFeedbackCat>> getAll(){
        List<TbFeedbackCat> list = feedbackCatService.getAll();
        return new ResultUtil<List<TbFeedbackCat>>().setData(list);
    }

}
